package entity

import (
	"strconv"
	"time"

	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

type YgddCompLoanDtip struct {
	ID    int        `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	ACNO  string     `gorm:"column:ACNO"`
	BANO  string     `gorm:"column:BANO"`
	CUNM  string     `gorm:"column:CUNM"`
	VLDT  *time.Time `gorm:"column:VLDT"`
	CYNM  string     `gorm:"column:CYNM"`
	ACBL  float64    `gorm:"column:ACBL"`
	KHJL1 string     `gorm:"column:KHJL1"` //客户经理1
	ZB1   *int       `gorm:"column:ZB1"`   //占比1
	KHJL2 string     `gorm:"column:KHJL2"` //客户经理2
	ZB2   *int       `gorm:"column:ZB2"`   //占比2
	KHJL3 string     `gorm:"column:KHJL3"` //客户经理3
	ZB3   *int       `gorm:"column:ZB3";`  //占比3
	KHJL4 string     `gorm:"column:KHJL4"` //客户经理4
	ZB4   *int       `gorm:"column:ZB4"`   //占比4
	KHJL5 string     `gorm:"column:KHJL5"` //客户经理5
	ZB5   *int       `gorm:"column:ZB5";`  //占比5
}

func (YgddCompLoanDtip) TableName() string {
	return "T_YGDD_COMP_LOAN_DTIP"
}

//导入时验证导入的占比是否为100%
func YgddCompLoanAfterValidation(index int, rowdatas []string) []base.ErrorData {
	var ZB1 float64
	var ZB2 float64
	var ZB3 float64
	var ZB4 float64
	var ZB5 float64
	if rowdatas[7] == "" {
		ZB1 = 0
	} else {
		ZB1, _ = strconv.ParseFloat(rowdatas[7], 64)
	}
	if rowdatas[9] == "" {
		ZB2 = 0
	} else {
		ZB2, _ = strconv.ParseFloat(rowdatas[9], 64)
	}
	if rowdatas[11] == "" {
		ZB3 = 0
	} else {
		ZB3, _ = strconv.ParseFloat(rowdatas[11], 64)
	}
	if rowdatas[13] == "" {
		ZB4 = 0
	} else {
		ZB4, _ = strconv.ParseFloat(rowdatas[13], 64)
	}
	if rowdatas[15] == "" {
		ZB5 = 0
	} else {
		ZB5, _ = strconv.ParseFloat(rowdatas[15], 64)
	}
	ZB1 += ZB2 + ZB3 + ZB4 + ZB5
	if ZB1 != 100 && ZB1 != 0 {
		errInfos := make([]base.ErrorInfo, 1, 1)
		errDatas := make([]base.ErrorData, 1, 1)
		errinfo := base.ErrorInfo{
			ColIndex: 7,
			Message:  "分配占比总额不等于100%",
		}
		errInfos[0] = errinfo
		errData := base.ErrorData{
			RowIndex:   index,
			DataValues: rowdatas,
			ErrorInfos: errInfos,
		}
		errDatas[0] = errData
		return errDatas
	}
	return nil
}

//导入后执行分户处理
func YgddCompLoanAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	YGDD := params["YGDD"]
	tx.Exec("EXEC UP_YGDD_COMP_LOAN ?", YGDD)
	//	tx.Exec("UPDATE T_YGDD_COMP_LOAN_DTIP SET YGDD=?", YGDD)
	//	tx.Exec("UPDATE T_SysUser_YGDD SET DGDKISDR=1 WHERE ID=?", YGDD)
	return nil
}

func YgddCompLoanDtipEF() base.EntityFunc {
	return func() interface{} {
		entity := YgddCompLoanDtip{}
		return &entity
	}
}
func YgddCompLoanDtipESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []YgddCompLoanDtip
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("YgddCompLoanDtip", base.NewDefaultDao(YgddCompLoanDtipEF(), YgddCompLoanDtipESF(), nil))
	base.SetValidationCallBack("YgddCompLoanDtip", YgddCompLoanAfterValidation)
	base.SetAfterCallBack("YgddCompLoanDtip", YgddCompLoanAfterImport)
}
